$(function () {
    propertyManager();
    function propertyManager() {
        $.ajax({
            url: ctx+'/rentcontent/selectAllProperty',
            data: {},
            type: 'GET',
            success: function (data) {
                var option = {
                    data: {
                        Thead: {attrName: "属性名称", frontName: "前台显示名", AttrContent: "属性内容"},
                        Tbody: [],
                    }
                };
                for (var i = 0; i < data.length; i++) {
                    (function () {
                        var index=i;
                        var Val = {
                            id: data[i].id,
                            attrName: data[i].propertyName ,
                            inputBtn: '',
                            CustomAddLabel: ''
                        };



                        var AddLabelS = $('<div class="AddLabelProperty"></div>');

                        Val.CustomAddLabel = AddLabelS;

                        var inputBtn=$('<div class="inputBtn"></div>');
                        var inputButOption={
                            data: {
                                fontName: data[i].viewName,
                                OkChange: function () {


                                    var inputBtnModule={
                                        data: {
                                            Title: '修改属性',
                                            Content: '',
                                            OkChange:function (e) {
                                                var viewNames=e.find('.InputText input').val();
                                                $.ajax({
                                                    url: ctx+'/rentcontent/updateProperty ',
                                                    data:{
                                                        id: data[index].id,
                                                        viewName:viewNames
                                                    },
                                                    type:'POST',
                                                    success:function (data) {
                                                        if(data.message=='修改成功'){
                                                            new $('body').Overlays({data:{text:'修改成功！',state:'Ok'}});
                                                            propertyManager();
                                                        }else {
                                                            new $('body').Overlays({data:{text:'修改失败，请稍后重试！',state:'No'}});
                                                            propertyManager();
                                                        }
                                                    }
                                                })
                                            }
                                        }
                                    };
                                    var inputBtnModuleContent=$('<div class="inputBtnModuleContent"><div class="rows"><div class="name">显示名：</div><div class="InputText"><input type="text" placeholder="请输入前台显示名" value="'+data[index].viewName+'"></div></div></div>');
                                    inputBtnModule.data.Content=inputBtnModuleContent;
                                    new $('body').ModalBox(inputBtnModule)

                                }
                            }
                        };

                        inputBtn.ReviseInput(inputButOption);
                        Val.inputBtn=inputBtn;



                        var pid=data[index].id;
                        $.ajax({
                            url:ctx+'/rentcontent/selectContentByPid',
                            data:{
                                pid:pid
                            },
                            type:'GET',
                            success:function (data) {
                                var AddLabelOption = {
                                    data: {
                                        placement:[],
                                    },
                                    AddButtonAction: function (objs) {
                                        var obj = {
                                            data: {
                                                Title: '属性内容添加',
                                                Content: '<div class="addAttr"><label><span>内容名称:</span><input type="text" placeholder="请输入内容"></label></div>',
                                                OkChange: function (e,remove) {
                                                    if(e.find('.addAttr input').val()&&!e.find('.addAttr input').val()==''){
                                                        $.ajax({
                                                            url:ctx+'/rentcontent/insertContent',
                                                            data:{
                                                                pid:pid,
                                                                contentName:e.find('.addAttr input').val()
                                                            },
                                                            type:'POST',
                                                            success:function (data) {
                                                                if(data.message=='创建成功'){
                                                                    new $('body').Overlays({data:{text:'添加成功！',state:'Ok'}});
                                                                    remove();
                                                                    propertyManager();
                                                                }else {
                                                                    new $('body').Overlays({data:{text:'添加失败，请稍后重试！',state:'No'}});
                                                                    propertyManager();
                                                                    remove();
                                                                }

                                                            }
                                                        })
                                                    }else {
                                                        new $('body').Overlays({data:{text:'输入信息有误，请检查后重试！',state:'No'}});
                                                    }
                                                }
                                            },
                                            OKHide:true
                                        };
                                        $('body').ModalBox(obj);
                                    }
                                };
                                if(data.length>=1){
                                    for(var i=0;i<data.length;i++){
                                        (function () {
                                            AddLabelOption.data.placement.push({id:data[i].id,name:data[i].contentName});
                                            AddLabelOption.data.placement['onChange']=function (e) {
                                                var id=e;
                                                $.ajax({
                                                    url:ctx+'/rentcontent/deleteContent',
                                                    data:{
                                                        id:id
                                                    },
                                                    type:'POST',
                                                    success:function (data) {
                                                        if(data.message=='删除成功'){
                                                            new $('body').Overlays({data:{text:'删除成功！',state:'Ok'}});
                                                            propertyManager();
                                                        }else {
                                                            new $('body').Overlays({data:{text:data.message,state:'No'}});
                                                            propertyManager();
                                                        }
                                                    }
                                                })
                                            }
                                        })()
                                    }

                                }
                                AddLabelS.AddLabel(AddLabelOption);
                            }
                        });

                        option.data.Tbody.push(Val);
                    })()

                };
                $(".Propertymanagement").Order(option);
            }
        });


    }
});




